package com.cloud.server.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cloud.common.Md5Utils;
import com.cloud.server.domain.user.User;
import com.cloud.server.from.login.FromLoginUser;
import com.cloud.server.service.user.LoginService;
import com.cloud.server.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author liuyuan
 * @Date 2021/11/23 8:52 上午
 * @Version 1.0
 */
@Service("mobilePassService")
public class MobilePassServiceImpl implements LoginService {

    @Autowired
    UserService userService;


    @Override
    @Transactional
    public User login(FromLoginUser user) throws Exception {
        String mobile = user.getMobile();
        if(mobile == null){
            throw new Exception("手机号不能为空");
        }
        String fromPassword = user.getPassword();
        if(fromPassword == null){
            throw new Exception("密码不能为空");
        }
        User queryUser = new User();
        queryUser.setMobile(mobile);
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.setEntity(queryUser);
        User one = userService.getOne(userQueryWrapper);
        if(one == null){
            throw new Exception("没有此用户");
        }
        String password = one.getPassword();
        boolean saltVerifyMD5 = Md5Utils.getSaltverifyMD5(fromPassword, password);
        if(saltVerifyMD5){
            return one;
        }else {
            throw new Exception("密码错误");
        }
    }
}
